//
//  NSString+TimeStamp.m
//  XinCap
//
//  Created by xinchuang1 on 15/2/4.
//  Copyright (c) 2015年 Roman Efimov. All rights reserved.
//
//  时间戳

#import "NSString+TimeStamp.h"
#import "Util.h"
@implementation NSString_TimeStamp

//android可用时间戳，取到毫秒
+ (NSString *)theTimeStamp//时间戳
{
    //获取当前时间 （当前时区）
    NSDate *date = [NSDate date];

    NSString *timeStamp = [NSString stringWithFormat:@"%lld",[self getDateTimeTOMilliSeconds:date]];

    return timeStamp;
}

+ (NSDate *)getDateTimeFromMilliSeconds:(NSNumber *) miliSecondnumber
{
    NSTimeInterval seconds = [miliSecondnumber doubleValue] / 1000;
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:seconds];
    return endDate;
}

+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime
{
    NSTimeInterval interval = [datetime timeIntervalSince1970];
//    NSLog(@"interval=%f",interval);
    long long totalMilliseconds = interval ;
    //    NSLog(@"totalMilliseconds=%llu",totalMilliseconds);
    return totalMilliseconds;
}


//计算时间间隔
//@paaram   paramStartDate            用于转换的时间
+ (NSString *)calDateTimeFromData:(NSDate *)paramStartDate{
    
    //获取当前时间
    NSDate *date = [NSDate date];//源日期
    /*
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];//得到源日期与世界标准时间的偏移量
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];//以当前NSDate对象为基准，偏移多少秒后得到的新NSDate对象
    NSLog(@"enddate=%@",localeDate);
     
    
    //转化为单前时区时间
    NSTimeZone *startZone = [NSTimeZone systemTimeZone];//源日期
    NSInteger startInterval = [startZone secondsFromGMTForDate: paramStartDate];//得到源日期与世界标准时间的偏移量
    NSDate *startLocaleDate = [paramStartDate  dateByAddingTimeInterval: startInterval];
    
    NSLog(@"\n 当前时间 %@：\n 发表时间：%@",date,startLocaleDate);
     */
    
    NSString *strResult=nil;
    
    NSCalendar *chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSUInteger unitFlags =  NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond | NSCalendarUnitDay| NSCalendarUnitMonth | NSCalendarUnitYear;
    
    NSDateComponents *DateComponent = [chineseClendar components:unitFlags fromDate:paramStartDate toDate:date options:0];
    
    NSInteger diffHour = [DateComponent hour];
    
    NSInteger diffMin    = [DateComponent minute];
    
    NSInteger diffSec   = [DateComponent second];
    
    NSInteger diffDay   = [DateComponent day];
    
    NSInteger diffMon  = [DateComponent month];
    
    NSInteger diffYear = [DateComponent year];
    
    if (diffYear>0) {
        strResult=[NSString stringWithFormat:@"%ld年前",(long)diffYear];
        return strResult;
    }else if(diffMon>0){
        strResult=[NSString stringWithFormat:@"%ld月前",(long)diffMon];
        return strResult;
    }else if(diffDay>0){
        strResult=[NSString stringWithFormat:@"%ld天前",(long)diffDay];
        return strResult;
    }else if(diffHour>0){
        strResult=[NSString stringWithFormat:@"%ld小时前",(long)diffHour];
        return strResult;
    }else if(diffMin>0){
        strResult=[NSString stringWithFormat:@"%ld分钟前",(long)diffMin];
        return strResult;
    }else if(diffSec>0){
        strResult=[NSString stringWithFormat:@"%ld秒前",(long)diffSec];
        return strResult;
    }else{
        strResult=[NSString stringWithFormat:@"未知时间"];
        return strResult;
    }
}



+ (NSString *) getDateTimeFromMilliSeconds:(NSNumber *) miliSecondnumber DateFormat:(NSString *)formatString  //将时间戳转转为时间字符串;
{
    NSDate *theDate = [NSString_TimeStamp getDateTimeFromMilliSeconds:miliSecondnumber];
    NSString *theDateString = [NSString_TimeStamp getDateStringWithDate:theDate DateFormat:formatString];
    return theDateString;
}



+ (NSString *)getDateStringWithDate:(NSDate *)date
                         DateFormat:(NSString *)formatString
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:formatString];
    NSString *dateString = [dateFormat stringFromDate:date];
//    NSLog(@"date: %@", dateString);
    return dateString;
}
//获取当前时间与未来间隔

+ (NSString *)calDateTimeLeftData:(NSDate *)paramStartDate
{
    //获取当前时间
    NSDate *date = [NSDate date];//源日期
    
    /*
   NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];//得到源日期与世界标准时间的偏移量
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];//以当前NSDate对象为基准，偏移多少秒后得到的新NSDate对象
    NSLog(@"enddate=%@",localeDate);
    
    //转化为单前时区时间
    NSTimeZone *startZone = [NSTimeZone systemTimeZone];//源日期
    NSInteger startInterval = [startZone secondsFromGMTForDate: paramStartDate];//得到源日期与世界标准时间的偏移量
    NSDate *startLocaleDate = [paramStartDate  dateByAddingTimeInterval: startInterval];
  
    NSLog(@"\n 当前时间 %@：\n 发表时间：%@",date,startLocaleDate);
     */
    
    NSString *strResult=nil;
    
    NSCalendar *chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSUInteger unitFlags = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay| NSCalendarUnitMonth | NSCalendarUnitYear;
    
    NSDateComponents *DateComponent = [chineseClendar components:unitFlags fromDate:date toDate:paramStartDate options:NSCalendarMatchStrictly];
    
    

    NSInteger diffSec   = [DateComponent second];
    
    NSInteger diffMin   = [DateComponent minute];
    
    NSInteger diffHour  = [DateComponent hour];
    
    NSInteger diffDay   = [DateComponent day];
    
    NSInteger diffMon   = [DateComponent month];
    
    NSInteger diffYear  = [DateComponent year];
    
    if (diffYear>0) {
        strResult=[NSString stringWithFormat:@"%ld年%ld月%ld天%ld小时",(long)diffYear,(long)diffMon,(long)diffDay,(long)diffHour];
        return strResult;
    }else if(diffMon>0){
        strResult=[NSString stringWithFormat:@"%ld月%ld天%ld小时",(long)diffMon,(long)diffDay,(long)diffHour];
        return strResult;
    }else if(diffDay>0){
        strResult=[NSString stringWithFormat:@"%ld天%ld小时",(long)diffDay,(long)diffHour];
        return strResult;
    }else if(diffHour>0){
        strResult=[NSString stringWithFormat:@"%ld小时",(long)diffHour];
        return strResult;
    }else if(diffMin>0){
        strResult=[NSString stringWithFormat:@"%ld分钟",(long)diffMin];
        return strResult;
    }else if(diffSec>0){
        strResult=[NSString stringWithFormat:@"%ld秒",(long)diffSec];
        return strResult;
    }else{
        strResult=[NSString stringWithFormat:@"已截止"];
        return strResult;
    }
    

}

@end
